public class 第六题2 {
    public static int count=0;
    public static void main(String[] args) {
        getPermutation("000000011111");
        System.out.println(count);
    }

    public static void getPermutation(String A) {
        char[] array = A.toCharArray();
        getPermutationCore(array, 0);
    }

    public static boolean isSame(char[] arr, int first, int end) {
        for (int i = first; i < end; i++) {
            if (arr[i] == arr[end]) {
                return false;
            }
        }
        return true;
    }

    public static boolean check(char[] array) {
        int[][] g = new int[3][4];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (array[i * 4 + j] == '1') {
                    g[i][j] = 1;
                } else {
                    g[i][j] = 0;
                }
            }
        }
        int count1 = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (g[i][j] == 1) {
                    dfs(g, i, j);
                    count1++;
                }
            }
        }
        return count1 == 1;
    }

    private static void dfs(int[][] g, int i, int j) {
        g[i][j] = 0;
        if (i - 1 >= 0 && g[i - 1][j] == 1) dfs(g, i - 1, j);
        if (i + 1 <= 2 && g[i + 1][j] == 1) dfs(g, i + 1, j);
        if (j - 1 >= 0 && g[i][j - 1] == 1) dfs(g, i, j - 1);
        if (j + 1 <= 3 && g[i][j + 1] == 1) dfs(g, i, j + 1);
    }


    public static void getPermutationCore(char[] arr, int k) {
        if (arr.length == k) {
            if(check(arr)){
                count++;
            }
            //System.out.println(new String(arr));
            return;
        }

        for (int i = k; i < arr.length; i++) {
            if (isSame(arr, k, i)) {
                swap(arr, k, i);
                getPermutationCore(arr, k + 1);
                swap(arr, k, i); // 回溯
            }
        }
    }

    public static void swap(char[] arr, int k, int i) {
        char tem = arr[k];
        arr[k] = arr[i];
        arr[i] = tem;
    }
}
